﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;
using System.IO;
using System.Collections;


/// <summary>
/// Class1 的摘要说明
/// </summary>
public class Cls_Config
{
  private Hashtable m_hashtable;

  public Cls_Config(string filename)
	{
    filename = System.Web.HttpContext.Current.Server.MapPath(filename);
    if (File.Exists(filename))
    {
      this.Initialize(filename);      
    }
    else
    {
      System.Web.HttpContext.Current.Response.Write("配置文件不存在！");
      return;
    }
	}

  private void Initialize(string filename) 
  {
    XmlTextReader reader = null;    
    try
    {
      string key = null, value = null;
      reader = new XmlTextReader(filename);
      reader.WhitespaceHandling = WhitespaceHandling.None;
      reader.MoveToContent();
      m_hashtable = new Hashtable();
      while (reader.Read())
      {
        if (3 == reader.Depth && XmlNodeType.EndElement == reader.NodeType && "Item" == reader.Name)
        {
          if (!m_hashtable.ContainsKey(key))
          {
            m_hashtable.Add(key, value);
          }
        }
        else if (4 == reader.Depth && reader.IsStartElement())
        {
          if ("Name" == reader.Name)
          {
            key = reader.ReadString();
          }
          else if ("Value" == reader.Name)
          {
            value = reader.ReadString();
          }
        }
      }
      key = value = null;
    }
    finally
    {
      if (null != reader)
      {
        reader.Close();
        reader = null;
      }
    }
    
  }

  //索引
  public string this[string key]
  {
    get
    {
      if (m_hashtable.ContainsKey(key))
      {
        return m_hashtable[key].ToString();
      }
      return string.Empty;
    }
    set
    {
      m_hashtable[key] = value;
    }
  }

}
